package com.heima.xxljob.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class HelloJob {

    @XxlJob("demoJobHandler")
    public void demoJobHandler()
    {
        XxlJobHelper.log("XXL-JOB, Hello World.");
        System.out.println("简单任务执行...");
    }

    @XxlJob("shardingJobHandler")
    public void shardingJobHandler()
    {
        // 分片的参数
        int shardIndex = XxlJobHelper.getShardIndex(); // 实例在集群中的序号
        int shardTotal = XxlJobHelper.getShardTotal(); // 集群总量

        // 业务逻辑
        List<Integer> list = getList();
        for (Integer integer : list) {
            if(integer % shardTotal == shardIndex){
                System.out.println("当前分片: "+shardIndex+", 当前任务项: "+integer);
            }
        }
    }

    public List<Integer> getList()
    {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            list.add(i);
        }
        return list;
    }
}
